package com.tagtraum.perf.gcviewer;

import com.google.common.collect.Lists;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SidePanel extends JPanel
{
  private static final Icon DOWN_ARROW = new ImageIcon(SidePanel.class.getResource("Arrow_down_12x12.png"));
  private static final Icon RIGHT_ARROW = new ImageIcon(SidePanel.class.getResource("Arrow_right_12x12.png"));
  private final List<SectionGroup> views;
  private static final Color TEXT_COLOR = Color.decode("#666666");

  public SidePanel(SectionsModel analytics, SectionsModel data, SectionsModel feedback)
  {
    this.views = Lists.newArrayList();
    renderViews(analytics, data, feedback);

    add(Box.createVerticalGlue());
    setBackground(Color.WHITE);
  }

  private void renderViews(SectionsModel analytics, SectionsModel data, SectionsModel feedback) {
    setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
    setLayout(new BoxLayout(this, 1));
    add(Box.createVerticalStrut(10));
    this.views.add(makeSectionsView("ANALYTICS", analytics, true));
    add(Box.createVerticalStrut(10));
    this.views.add(makeSectionsView("GRAPHS AND DATA", data, false));
    add(Box.createVerticalStrut(10));
    this.views.add(makeSectionsView("BUGS AND FEEDBACK", feedback, false));
  }

  private SectionGroup makeSectionsView(String title, SectionsModel model, boolean top) {
    SectionGroup view = new SectionGroup(model, false);
    view.setAlignmentX(0.0F);
    GroupHeader header = new GroupHeader(title, view, top);
    add(header);
    add(view);
    return view;
  }

  private class GroupHeader extends JLabel {
    private boolean isExpanded;
    private final String title;
    private final SectionGroup view;
    private Component filler;
    private final boolean top;

    public GroupHeader(String title, SectionGroup view, boolean top) {
      this.title = title;
      this.view = view;
      this.top = top;
      this.isExpanded = true;
      setFont(new Font("Arial", 1, 15));
      setForeground(SidePanel.TEXT_COLOR);
      setHorizontalAlignment(2);
      setAlignmentX(0.0F);

      addMouseListener(new MouseAdapter()
      {
        public void mouseClicked(MouseEvent event) {
          SidePanel.GroupHeader.this.onClick();
        }
      });
      updateTitle();
    }

    public void onClick() {
      this.isExpanded = (!this.isExpanded);
      updateTitle();
      Dimension size = this.view.getSize();
      if (!this.top) {
        if (this.isExpanded) {
          SidePanel.this.remove(this.filler);
        } else {
          this.filler = Box.createVerticalStrut((int)size.getHeight());
          SidePanel.this.add(this.filler);
        }
      }
      this.view.setVisible(this.isExpanded);
    }

    private void updateTitle() {
      Icon icon = this.isExpanded ? SidePanel.DOWN_ARROW : SidePanel.RIGHT_ARROW;
      setIcon(icon);
      setText(this.title);
    }
  }
}